package com.csair.seam.domain.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csair.seam.domain.mapper.SysUserMapper;
import com.csair.seam.domain.model.entity.SysUser;
import com.csair.seam.domain.model.param.SysUserParam;
import com.csair.seam.domain.service.IEhomeService;
import com.csair.seam.domain.service.ISysUserService;
import com.csair.seam.infrastructure.ehome.EhomeParamData;
import com.csair.seam.infrastructure.ehome.EhomeResult;
import com.csair.seam.infrastructure.exception.BizException;
import com.csair.seam.infrastructure.web.user.UserInfo;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private IEhomeService ehomeService;

    /**
     * 1、验证是否在缓存是否存在，不存在查找E家
     * 2、存在后，验证是否本地系统有账号，生产需要调用的方法
     **/
    @Override
    public UserInfo checkUserInfo(EhomeParamData param) {
    	EhomeResult result = ehomeService.checkLogin(param.getLoginAccount(),param.getDevice(),param.getToken());
        if(!result.getResult().equals("0")){
            throw new BizException(result.getMessage());
        }
        SysUser sysUser = this.queryUserByAccount(param.getLoginAccount());
        UserInfo userInfo;
        if(sysUser==null){
            userInfo = new UserInfo(param.getLoginAccount());
        } else {
            userInfo = new UserInfo(sysUser);
        }
        return userInfo;
    }

    @Override
    public SysUser queryUserByAccount(String account) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getAccount, account);
        return getOne(queryWrapper,false);
    }

	@Override
	public List<SysUser> queryUserByParams(SysUserParam param) {
//		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
//		queryWrapper.lambda()
//						.eq(SysUser::getOrgId, param.getOrgId())
//						.and(i-> i.like(SysUser::getName, param.getUserNameOrCode()).or().eq(SysUser::getAccount, param.getUserNameOrCode()))
//						;
//		return getBaseMapper().selectList(queryWrapper);
		return getBaseMapper().selectMyDeptUsers(param);
	}

}
